package org.mclll.xwangplugin.town.command.executor;

import com.alibaba.fastjson.JSON;
import org.bukkit.command.CommandSender;
import org.mclll.xwangplugin.town.database.domain.TownDO;
import org.mclll.xwangplugin.town.database.mapper.manage.TownManage;

import java.util.List;

import static org.mclll.xwangplugin.town.config.TownContext.*;

public class TownSetDesc {

    public static void setDesc(CommandSender sender, String[] args) {
        if (args.length != 3) {
            sender.sendMessage(TOWN_SET_DESC_FORMAT);
            return;
        }
        String townName = args[1];
        TownDO townQueryParam = new TownDO();
        townQueryParam.setName(townName);
        List<TownDO> townList = TownManage.selectList(townQueryParam);
        if (townList.isEmpty()) {
            sender.sendMessage(TOWN_NOT_FOUND);
            return;
        }
        TownDO town = townList.get(0);
        TownDO.StarDoor starDoor = town.getStarDoor();
        starDoor.setDesc(args[2]);
        TownDO townUpdateParam = new TownDO();
        townUpdateParam.setUuid(town.getUuid());
        townUpdateParam.setStarDoorJson(JSON.toJSONString(starDoor));
        TownManage.updateByUuid(townUpdateParam);
        sender.sendMessage(String.format(TOWN_SET_DESC_SUCCESS, townName));
    }
}
